<?php
include("../includes/db_connect.php");
	
if(isset($_POST["submit"])) {
	$id = safety($_POST["id"]);
	$pageQuery = mysqli_query($db, "DELETE FROM pages WHERE id='$id'");
	echo "<div id=\"result\">";
	if($pageQuery)
		echo "Raderade sidan med id: $id";
	else
		echo "Kunde inte radera sidan med id: $id";
	echo "</div>";
}else{
	echo "<div id=\"result\"></div>";
	
	echo "<form class=\"deleteForm\" action=\"\" method=\"POST\">";
	echo "<table id=\"pageList\">";
	createPageList($db);
	echo "<tr><td><input type=\"submit\"name=\"submit\" value=\"Ta bort\"></td></tr>";
	echo "</table>";
	echo "</form>";
	echo "<div id=\"result\"></div>";
}
?>

<script src="script/jquery-1.11.1.min.js"></script>
<script src="script/ajaxlinks.js"></script>
<script>
var _SESSION = {};
_SESSION["currentPage"] = "deletePage.php";

var id = 0;
var lastClicked = null;
$('.pageList').click(function() { 
	id = this.id.substring(2);
	this.style.backgroundColor = "lightgrey";
	if(lastClicked != null)
		lastClicked.style.backgroundColor = "white";
	lastClicked = this;
});
		
$('.pageList').hover(function(){
	$('.pageList').css("cursor", "pointer");	
});

$('form.deleteForm').submit(function (e) {
	e.preventDefault();
	var url = "submit=true";
	url += "&id="+id;
	
	setTimeout(function(){ 		
		var page = getPage(_SESSION["currentPage"], "POST", url);
		var tmpDiv = document.createElement("div");
		tmpDiv.innerHTML = page;
		document.getElementById("result").innerHTML = tmpDiv.querySelector("[id=result]").innerHTML;
	}, 1);
	
	return false;

});
</script>